package org.glut.AppStoreProject.service;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.glut.AppStoreProject.entity.Download;
import org.glut.AppStoreProject.mapper.DownloadMapper;
import org.glut.AppStoreProject.utils.MybatisUtil;

import java.util.List;

/**
 * @author: gluglu2
 * @date: 2025/7/18 10:47
 */
public class DownloadServiceImpl {
    static Logger logger = Logger.getLogger(DownloadServiceImpl.class);

    /***
     * @description: 插入download信息
     * @param download
     * @return boolean
     * @author: gluglu2
     * @date: 2025/7/18 10:53
     */
    public boolean insertDownload(Download download) {
        try (
                SqlSession session = MybatisUtil.getSession();
        ) {
            DownloadMapper mapper = session.getMapper(DownloadMapper.class);
            int result = mapper.insertDownload(download);
            session.commit();
            return result > 0;
        } catch (Exception e) {
            logger.error("插入download信息失败",e);
        }
        return false;
    }

    /***
     * @description: 查询download信息
     * @param download
     * @return org.glut.AppStoreProject.entity.Download
     * @author: gluglu2
     * @date: 2025/7/18 10:57
     */
    public List<Download> getDownloadByUserId(Download download) {
        try (
                SqlSession session = MybatisUtil.getSession();
        ) {
            DownloadMapper mapper = session.getMapper(DownloadMapper.class);
            return mapper.getDownloads(download);
        } catch (Exception e) {
            logger.error("查询指定user的download信息失败",e);
        }
        return null;
    }

    public boolean uninstallDownload(Download download) {
        try (
                SqlSession session = MybatisUtil.getSession();
        ) {
            DownloadMapper mapper = session.getMapper(DownloadMapper.class);
            int result = mapper.uninstallDownloads(download);
            session.commit();
            return result > 0;
        } catch (Exception e) {
            logger.error("删除指定user的download信息失败",e);
        }
        return false;
    }
}
